﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.IO;
using ExamAnalysis.Core;
using ExamAnalysis.Model;

namespace ExamAnalysis.Manage
{
    public partial class StudentInExamItem : System.Web.UI.Page
    {
        static int examitemid = 0;
        static int studentid = 0;
        static int reasonid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var mySIEI = from siei in db.StudentInExamItem
                                 select new { ReasonName = siei.ReasonInfo.ReasonName, ReasonID = siei.ReasonID, ExamItemID = siei.ExamItemID, StudentID = siei.StudentID, StudentCode = siei.StudentInfo.StudentCode, StudentName = siei.StudentInfo.StudentName, Score = siei.Score };
                    this.Store2.DataSource = mySIEI;
                    this.Store2.DataBind();
                    var myExamItem = from ei in db.ExamItems
                                     join ex in db.ExamInfo on ei.ExamItemsGroup.ExamID equals ex.ExamID
                                     select new { ExamItemID = ei.ExamItemID, ItemName = ei.ItemName, ItemType = ei.ItemType, ExamName = ex.ExamName };
                    this.Store1.DataSource = myExamItem;
                    this.Store1.DataBind();
                    this.Store3.DataSource = myExamItem;
                    this.Store3.DataBind();
                }
            }
        }

        protected void Button_1_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            examitemid = 0;
            RowSelectionModel sm = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                examitemid = Convert.ToInt32(row.RecordID);
            }

            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var myExamItem = from ei in db.ExamItems
                              where ei.ExamItemID==examitemid
                              select ei.ItemName;
                foreach (var examitem in myExamItem)
                {
                    DropDownField2.Text = examitem.ToString();
                }
                var mySIEI = from siei in db.StudentInExamItem
                             where siei.ExamItemID == examitemid
                             select new { ReasonName = siei.ReasonInfo.ReasonName, ReasonID = siei.ReasonID, ExamItemID = siei.ExamItemID, StudentID = siei.StudentID, StudentCode = siei.StudentInfo.StudentCode, StudentName = siei.StudentInfo.StudentName, Score = siei.Score };
                this.Store2.DataSource = mySIEI;
                this.Store2.DataBind();
            }
        }

        protected void Button2_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            examitemid = 0;
            studentid = 0;
            reasonid = 0;
            this.FormWindow.Show();
        }

        protected void ExamItem_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            examitemid = 0;
            RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                examitemid = Convert.ToInt32(row.RecordID);
            }

            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var myExamItem = from ei in db.ExamItems
                                 where ei.ExamItemID == examitemid
                                 select ei.ItemName;
                foreach (var examitem in myExamItem)
                {
                    DropDownField1.Text = examitem.ToString();
                }
            }
        }

        protected void Student_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            studentid = 0;
            RowSelectionModel sm = this.GridPanel3.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                studentid = Convert.ToInt32(row.RecordID);
            }

            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var newStudent = from s in db.StudentInfo
                                 where s.StudentID == studentid
                                 select s.StudentName;
                foreach (var student in newStudent)
                {
                    DropDownField3.Text = student.ToString();
                }
            }
        }

        protected void Reason_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            reasonid = 0;
            RowSelectionModel sm = this.GridPanel4.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                reasonid = Convert.ToInt32(row.RecordID);
            }
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var myReason = from r in db.ReasonInfo
                               where r.ReasonID == reasonid
                               select r.ReasonName;
                foreach (var reason in myReason)
                {
                    DropDownField4.Text = reason.ToString();
                }
            }
        }

        protected void Add_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            if (DropDownField1.Text.ToString().Equals("请选择") || DropDownField3.Text.ToString().Equals("请选择") || DropDownField4.Text.ToString().Equals("请选择"))
            {
                X.Msg.Alert("提示信息", String.Format("请保持信息完整")).Show();
            }
            else
            {
                ExamAnalysis.Model.StudentInExamItem newsiei = new ExamAnalysis.Model.StudentInExamItem();
                newsiei.ExamItemID = examitemid;
                newsiei.StudentID = studentid;
                newsiei.Score = Convert.ToDecimal(Score1.Text);
                newsiei.ReasonID = reasonid;
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var mySIEI = from siei in db.StudentInExamItem
                                 where siei.ExamItemID == examitemid & siei.StudentID == studentid
                                 select siei;
                    if (mySIEI.Count() == 0)
                    {
                        db.StudentInExamItem.InsertOnSubmit(newsiei);
                        db.SubmitChanges();
                        X.Msg.Alert("提示信息", String.Format("成功添加答题")).Show();
                    }
                    else
                    {
                        ExamAnalysis.Model.StudentInExamItem mysiei = mySIEI.First();
                        mysiei.Score = Convert.ToDecimal(Score1.Text);
                        mysiei.ReasonID = reasonid;
                        db.SubmitChanges();
                        X.Msg.Alert("提示信息", String.Format("成功修改答题")).Show();
                    }
                    studentid = 0;
                    reasonid = 0;
                    examitemid = 0;
                }
                Response.Redirect("StudentInExamItem.aspx");
            }
        }

        protected void Delete_Click(object sender, Ext.Net.DirectEventArgs e)
        {

            if (DropDownField1.Text.ToString().Equals("请选择") || DropDownField3.Text.ToString().Equals("请选择"))
            {
                X.Msg.Alert("提示信息", String.Format("请保持信息完整")).Show();
            }
            else
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var mySIEI = from siei in db.StudentInExamItem
                                 where siei.ExamItemID == examitemid & siei.StudentID == studentid
                                 select siei;
                    if (mySIEI.Count() != 0)
                    {
                        db.StudentInExamItem.DeleteOnSubmit(mySIEI.First());
                        db.SubmitChanges();
                        X.Msg.Alert("提示信息", String.Format("删除成功")).Show();
                        studentid = 0;
                        reasonid = 0;
                        examitemid = 0;
                        Response.Redirect("StudentInExamItem.aspx");
                    }
                    else
                    {
                        X.Msg.Alert("提示信息", String.Format("不存在此记录")).Show();
                    }
                }
            }
            }
        
    }
}